package com.zlm.security;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.servlet.http.HttpServletRequest;

/**
 * 自定义记住我 service实现类
 */

public class MyPersistentTokenBasedRememberMeServices extends PersistentTokenBasedRememberMeServices {

    public MyPersistentTokenBasedRememberMeServices(String key, UserDetailsService userDetailsService, PersistentTokenRepository tokenRepository) {
        super(key, userDetailsService, tokenRepository);
    }

    /**
     * 自定义前后端分离的获取 remember-me的方式
     * 因为原本的是从cookie里面获取的
     * 现在前后端分离了，是以json格式传递过来的
     *
     * @param request
     * @param parameter
     * @return
     */
    @Override
    protected boolean rememberMeRequested(HttpServletRequest request, String parameter) {
        String rememberMeValue = request.getAttribute(parameter).toString();
        if (rememberMeValue != null) {
            if (rememberMeValue.equalsIgnoreCase("true") || rememberMeValue.equalsIgnoreCase("on")
                    || rememberMeValue.equalsIgnoreCase("yes") || rememberMeValue.equals("1")) {
                return true;
            }
        }
        return false;
    }
}
